//
//  ChatListController.m
//  projectOfTeamSample
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 liukai. All rights reserved.
//

#import "ChatListController.h"
#import "ChatListTableViewCell.h"
#import "ChatTableViewController.h"
#import "ChatAddressBookCell.h"
@interface ChatListController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UISegmentedControl *segmentedControl;

@end

@implementation ChatListController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"小纸条";
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
   
    //一个segmented
    [self setSegmented];
    
    //注册
    [self.tableView registerClass:[ChatListTableViewCell class]    forCellReuseIdentifier:@"ChatList"];
    [self.tableView registerClass:[ChatAddressBookCell class] forCellReuseIdentifier:@"ChatAddressBook"];
    


}
#pragma mark -- Segmented
//setSegmented
-(void)setSegmented{
    
    CGRect frame = [UIScreen mainScreen].bounds;
    
    self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 44, frame.size.width, 30)];
    
    self.segmentedControl = [[UISegmentedControl alloc]initWithFrame:self.tableView.tableHeaderView.bounds];
    
    //添加分段
    [self.segmentedControl insertSegmentWithTitle:@"通讯录" atIndex:0 animated:YES];
    [self.segmentedControl insertSegmentWithTitle:@"聊天" atIndex:1 animated:YES];
    
    //设置初始的位置
    self.segmentedControl.selectedSegmentIndex = 1;
    
    //设置属性
    self.segmentedControl.tintColor = [UIColor grayColor];
    
    //为segment添加点击事件
    [self.segmentedControl addTarget:self action:@selector(segmentedDidCilckAction) forControlEvents:UIControlEventValueChanged];
    
    [self.tableView.tableHeaderView addSubview:self.segmentedControl];
}
//segmentedDidCilckAction:点击事件
- (void)segmentedDidCilckAction{
    switch (self.segmentedControl.selectedSegmentIndex) {
        case 0:
            [self.tableView registerClass:[ChatListTableViewCell class]    forCellReuseIdentifier:@"ChatList"];
            [self.tableView reloadData];
            break;
       
        case 1:
            [self.tableView registerClass:[ChatAddressBookCell class] forCellReuseIdentifier:@"ChatAddressBook"];
            [self.tableView reloadData];
            break;
        default:
            break;
    }
    
    NSLog(@"%ld",(long)self.segmentedControl.selectedSegmentIndex);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    if (self.segmentedControl.selectedSegmentIndex == 0) {
        ChatAddressBookCell *chatAdd = [tableView dequeueReusableCellWithIdentifier:@"ChatAddressBook" forIndexPath:indexPath];
        chatAdd.headImage.image = [UIImage imageNamed:@"2.png"];
        chatAdd.userName.text = @"赵钱钱";
        return chatAdd;
    }else{
        ChatListTableViewCell *chatlist = [tableView dequeueReusableCellWithIdentifier:@"ChatList" forIndexPath:indexPath];
        chatlist.userHeadImage.image = [UIImage imageNamed:@"1.png"];
        chatlist.userName.text = @"测试用户名";
        chatlist.userChat.text = @"测试聊天";
        return chatlist;
    }
    
    // Configure the cell...
    
    
    
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
//点击cell 的代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ChatTableViewController *chatCon = [[ChatTableViewController alloc]init];
    //吧当前用户的信息传给后一页
    chatCon.title = @"姓名:王小花";
    chatCon.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:chatCon animated:YES];
}
/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


@end
